#include "render.h"

void renderClear(SDL_Renderer **render){
    SDL_SetRenderDrawColor(*render, 0xff, 0xff, 0xff, 0xff);
    SDL_RenderClear(*render);
}

void renderClearEx(SDL_Renderer **render, SDL_Color *color){
    SDL_SetRenderDrawColor(*render, color->r, color->g, color->b, color->a);
    SDL_RenderClear(*render);
}

bool renderText(SDL_Renderer **render, SDL_Texture **texture, TTF_Font *font, SDL_Color *color, char *text, int x, int y){
    SDL_Surface *surface = TTF_RenderText_Solid(font, text, *color);
    if(!surface){
        printf("TTF render text solid failed: %s\n", TTF_GetError());
        return false;
    }
    *texture = SDL_CreateTextureFromSurface(*render, surface);
    if(!(*texture)){
        printf("SDL create texture from surface failed: %s\n", SDL_GetError());
        return false;
    }
    SDL_Rect ditRect = {x, y, surface->w, surface->h};
    SDL_FreeSurface(surface);
    SDL_RenderCopy(*render, *texture, NULL, &ditRect);
    SDL_DestroyTexture(*texture);
    *texture = NULL;
    return true;
}

bool renderImage(SDL_Renderer **render, Texture *tex, SDL_Rect *clip, int x, int y){
    SDL_Rect ditRect = {x, y, tex->w, tex->h};
    if(clip){
        ditRect.w = clip->w;
        ditRect.h = clip->h;
    }
    if(SDL_RenderCopy(*render, tex->texture, clip, &ditRect) != 0)
        return false;

    return true;
}

